package com.bnq.control.common.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bnq.control.R;

/**
 * Created by qianwei on 2017/8/7.
 */

public class LoadingDialog extends Dialog implements DialogInterface.OnShowListener {
    private static final String TAG = LoadingDialog.class.getSimpleName();
    private static final int TIME_OUT_MSG = 10008988;
    private OnTimeoutListener mOnTimeoutListener;
    private int timeOut;
    private View mView;
    private TextView mMessage;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case TIME_OUT_MSG:
                    if (isShowing()) {
                        if (mOnTimeoutListener != null)
                            mOnTimeoutListener.onTimeOut();
                        dismiss();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onShow(DialogInterface dialog) {
        if (timeOut != -1 && null != mHandler) {
            mHandler.sendEmptyMessageDelayed(TIME_OUT_MSG, timeOut);
        }
    }

    public interface OnTimeoutListener {
        void onTimeOut();
    }

    public LoadingDialog(Context context) {
        this(context, R.style.CustomDialogTheme);
    }

    private LoadingDialog(Context context, int mTheme) {
        super(context, mTheme);
        mView = LayoutInflater.from(context).inflate(R.layout.dialog_loading_view, null);
        mMessage = (TextView) mView.findViewById(R.id.tipTextView);
        timeOut = -1;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setOnShowListener(this);
    }

    public void setRequestFullScreen() {
        Window window = getWindow();
        if (null != window)
            window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    }


    public void setTips(CharSequence charSequence) {
        if (null != mMessage)
            mMessage.setText(charSequence);
    }

    public void setTips(int textId) {
        if (null != mMessage)
            mMessage.setText(textId);
    }

    /**
     * @param timeOut
     */
    public void setTimeOut(int timeOut) {
        this.timeOut = timeOut;
    }

    public void setOnTimeoutListener(OnTimeoutListener listener) {
        this.mOnTimeoutListener = listener;
    }

    public void dismissLoadingDialog() {
        if (null != mHandler) {
            mHandler.removeMessages(TIME_OUT_MSG);
            mOnTimeoutListener = null;
        }
        dismiss();
    }

    @Override
    public void dismiss() {
        if (null != mHandler) {
            mHandler.removeMessages(TIME_OUT_MSG);
            mOnTimeoutListener = null;
        }
        super.dismiss();
    }
}
